package com.zzb.myshowme.view.fragment;


import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.zzb.myshowme.R;
import com.zzb.myshowme.bean.UserInfo;
import com.zzb.myshowme.presenter.PhoneRegisterToMessagePresenter;
import com.zzb.myshowme.presenter.impl.PhoneRegisterToMessagePresenterImpl;
import com.zzb.myshowme.utils.CustomToast;
import com.zzb.myshowme.view.IView.PhoneRegisterToMessageView;
import com.zzb.myshowme.view.activity.MainActivity;
import com.zzb.myshowme.view.activity.SendSecurityCodeActivity;
import com.zzb.myshowme.wiget.ProgressDialogView;

/**
 * 注册 -> 填写用户信息
 * A simple {@link Fragment} subclass.
 */
public class PhoneRegisterToMessageFragment extends Fragment implements View.OnClickListener, PhoneRegisterToMessageView {

    PhoneRegisterToMessagePresenter phoneRegisterToMessagePresenter;

    View view;
    ImageView back_imageV;
    EditText nickname_editText, password_editText, password_again_editText;
    RelativeLayout man_layout, woman_layout;
    Button register_button;

    String sex = "男"; //默认男生

    boolean bool_nickname;
    boolean bool_password;
    boolean bool_password_again;

    public PhoneRegisterToMessageFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        phoneRegisterToMessagePresenter = new PhoneRegisterToMessagePresenterImpl(this);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_phone_regster_to_message, container, false);
        initView(view);
        return view;
    }

    private void initView(View view) {

        ProgressDialogView.Initial(getActivity(), "正在注册");

        back_imageV = (ImageView)view.findViewById(R.id.back_imageV);
        nickname_editText = (EditText) view.findViewById(R.id.nickname_editText);
        man_layout = (RelativeLayout) view.findViewById(R.id.man_layout);
        woman_layout = (RelativeLayout)view.findViewById(R.id.woman_layout);
        password_editText = (EditText) view.findViewById(R.id.password_editText);
        password_again_editText = (EditText) view.findViewById(R.id.password_again_editText);
        register_button = (Button) view.findViewById(R.id.register_button);

        back_imageV.setOnClickListener(this);
        man_layout.setOnClickListener(this);
        woman_layout.setOnClickListener(this);
        register_button.setOnClickListener(this);

        man_layout.setSelected(true);

        nickname_editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().length() > 0) {
                    bool_nickname = true;
                } else {
                    bool_nickname = false;
                }

                if (bool_nickname && bool_password && bool_password_again) {
                    register_button.setEnabled(true);
                } else {
                    register_button.setEnabled(false);
                }
            }
        });

        password_editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().length() > 0) {
                    bool_password = true;
                } else {
                    bool_password = false;
                }

                if (bool_nickname && bool_password && bool_password_again) {
                    register_button.setEnabled(true);
                } else {
                    register_button.setEnabled(false);
                }
            }
        });

        password_again_editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().length() > 0) {
                    bool_password_again = true;
                } else {
                    bool_password_again = false;
                }

                if (bool_nickname && bool_password && bool_password_again) {
                    register_button.setEnabled(true);
                } else {
                    register_button.setEnabled(false);
                }
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (view != null) {
            ViewGroup vg = (ViewGroup) view.getParent();
            vg.removeView(view);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back_imageV:
                getActivity().finish();
                break;
            case R.id.man_layout:
                selectSex(true);
                break;
            case R.id.woman_layout:
                selectSex(false);
                break;
            case R.id.register_button:
                register();
                break;
        }
    }

    /**
     * @param s true表示男性
     */
    private void selectSex(boolean s){
        if(s){
            sex = "男";
            man_layout.setSelected(true);
            woman_layout.setSelected(false);
        }else {
            sex = "女";
            man_layout.setSelected(false);
            woman_layout.setSelected(true);
        }
    }

    /**
     * 注册
     */
    private void register() {
        String phone = ((SendSecurityCodeActivity) getActivity()).phone;
        String nickname = nickname_editText.getText().toString();
        String password = password_editText.getText().toString();
        String password_again = password_again_editText.getText().toString();
        phoneRegisterToMessagePresenter.register(phone, nickname, sex, password, password_again);
    }

    @Override
    public void showProgress() {
        ProgressDialogView.show();
    }

    @Override
    public void hideProgress() {
        ProgressDialogView.dismiss();
    }

    /**--------------P 返回到 v 的 回调接口 --------------------***/

    @Override
    public void showToastError(String msg){
        CustomToast.show(getActivity(), msg, Toast.LENGTH_LONG);
    }

    @Override
    public void navigateToHome(UserInfo user){
        Intent intent = new Intent(getActivity(), MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);//开新堆栈，并清空之前的堆栈
        intent.putExtra("user", user);
        startActivity(intent);
    }
}
